import 'package:flutter/material.dart';

class ZsnProviders extends ChangeNotifier {
  bool videoInitialized = false; // 视频是否初始化
  bool pause = false; // 是否暂停
  bool isBuffering = false; // 是否缓冲
  bool showUI = false; // 是否显示UI
  Duration videoDuration = const Duration(); // 视频总时长
  Duration videoCurrentDuration = const Duration(); // 视频当前播放时长
  double second = 0; // 快退 快进的秒数
  Duration beforeProgress = const Duration(); // 快退 快进前的进度
  Duration afterProgress = const Duration(); // 快退 快进后的进度
  double currentVolume = 1.0; // 当前音量
  bool volumeUI = false; // 音量控制UI
  double currentBrightness = 1.0; // 当前亮度
  bool brightnessUI = false; // 亮度控制UI
  bool isLock = false; // 是否锁定
  String? errorMessage; // 添加错误信息属性

  // 数据重置
  ZsnProviders() {
    resetState();
  }
  void resetState() {
    isLock = false;
    showUI = false;
    errorMessage = null;
    notifyListeners();
  }

  void changePause() {
    pause = !pause;
    notifyListeners();
  }

  void setPause(bool isPause) {
    pause = isPause;
    notifyListeners();
  }

  void setBuffering(bool isBuffering) {
    this.isBuffering = isBuffering;
    notifyListeners();
  }

  void changeShowUI() {
    showUI = !showUI;
    notifyListeners();
  }

  void setShowUI(bool showUI) {
    this.showUI = showUI;
    notifyListeners();
  }

  void setVideoDuration(Duration videoDuration) {
    this.videoDuration = videoDuration;
    notifyListeners();
  }

  void setVideoCurrentDuration(Duration videoCurrentDuration) {
    this.videoCurrentDuration = videoCurrentDuration;
    notifyListeners();
  }

  void setVolume(double volume) {
    currentVolume = volume;
    notifyListeners();
  }

  void setVolumeUI(bool volumeUI) {
    this.volumeUI = volumeUI;
    notifyListeners();
  }

  void setBrightness(double brightness) {
    currentBrightness = brightness;
    notifyListeners();
  }

  void setBrightnessUI(bool brightnessUI) {
    this.brightnessUI = brightnessUI;
    notifyListeners();
  }

  void setSecond(double value) {
    second = second + value;
    notifyListeners();
  }

  void setBeforeProgress(Duration value) {
    beforeProgress = value;
    notifyListeners();
  }

  void setAfterProgress(Duration value) {
    afterProgress = value;
    notifyListeners();
  }

  void calculateAfterProgress() {
    afterProgress = beforeProgress + Duration(seconds: second.toInt());
    if (afterProgress > videoDuration) {
      afterProgress = videoDuration;
    }
    if (afterProgress < Duration.zero) {
      afterProgress = Duration.zero;
    }
    notifyListeners();
  }

  void resetSecond() {
    second = 0;
    notifyListeners();
  }

  void changeLock() {
    isLock = !isLock;
    notifyListeners();
  }

  void resetLock() {
    isLock = false;
    notifyListeners();
  }

  void setErrorMessage(String? message) {
    errorMessage = message;
    notifyListeners();
  }
}
